package cn.com.anypay.manager.controller.sdk;

import cn.com.anypay.manager.common.response.ApiResponse;
import cn.com.anypay.manager.service.OcrService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/ocr")
public class OcrController {

    @Resource
    private OcrService ocrService;

    @GetMapping("/config")
    public ApiResponse<Map<String, Object>> getCurrentConfig(@RequestParam(required = false) String provider) {
        log.info("获取OCR配置，provider: {}", provider);
        Map<String, Object> config;
        if (provider != null) {
            config = ocrService.getConfigByProvider(provider);
        } else {
            config = ocrService.getCurrentConfig();
        }
        log.info("获取OCR配置成功: {}", config);
        return ApiResponse.success(config);
    }

    @PostMapping("/switch")
    public ApiResponse<String> switchProvider(@RequestBody Map<String, Object> config) {
        String result = ocrService.switchProvider(config);
        return ApiResponse.success("切换OCR服务商成功", result);
    }

    @PostMapping("/recognize")
    public ApiResponse<?> recognize(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "serviceType", defaultValue = "general") String serviceType) {
        if (file == null || file.isEmpty()) {
            return ApiResponse.fail("请选择要上传的文件");
        }

        var result = ocrService.recognize(file, serviceType);
        return ApiResponse.success(result);
    }
}
